package org.shuma.productPriceType.impl;

import com.alipay.sofa.runtime.api.annotation.SofaService;
import com.alipay.sofa.runtime.api.annotation.SofaServiceBinding;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.shuma.productPriceType.domain.entity.ProductPriceType;
import org.shuma.productPriceType.exception.InsertProductPriceTypeException;
import org.shuma.productPriceType.exception.UpdateProductPriceException;
import org.shuma.productPriceType.facade.ProductPriceTypeFacade;
import org.shuma.productPriceType.mapper.PriceTypeMapper;
import org.shuma.productPriceType.mapper.ProductPriceTypeMapper;
import org.shuma.productPriceType.model.dto.ProductPriceTypeDTO;
import org.shuma.productPriceType.model.dto.UpdatePriceTypeDTO;
import org.shuma.productPriceType.model.entity.PriceType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

@Slf4j
@Service
@SofaService(interfaceType = ProductPriceTypeFacade.class, uniqueId = "productPriceType99", bindings = {@SofaServiceBinding(bindingType = "bolt")})
public class ProductPriceTypeImpl extends ServiceImpl<ProductPriceTypeMapper, ProductPriceType>
        implements ProductPriceTypeFacade {
    @Autowired
    ProductPriceTypeMapper productPriceTypeMapper;

    @Autowired
    PriceTypeMapper priceTypeMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean insertppt_Type(ProductPriceTypeDTO ppt) {
        List<ProductPriceType> products = productPriceTypeMapper.selectListById(ppt.getProductId());

        int money = 0, integral = 0, moneyIntegral = 0;
        for (ProductPriceType productPriceType : products) {
            Long id = productPriceType.getPriceTypeId();
            try {
                PriceType priceType = priceTypeMapper.selectOne(
                        new QueryWrapper<PriceType>().eq("ppt_id", id));
                if (priceType.getPptType() == 0) money = 1;
                if (priceType.getPptType() == 1) integral = 1;
                if (priceType.getPptType() == 2) moneyIntegral = 1;
            }catch (Exception exception) {
                throw new InsertProductPriceTypeException("添加失败");
            }
        }

        try {
            //有纯现金的兑换方式
            if (ppt.getPptMoney() != null && !ppt.getPptMoney().equals(0) && money == 0) {
                PriceType priceType = new PriceType(ppt.getPptMoney(), 0);
                priceType.setPptType(0);

                priceTypeMapper.insert(priceType);
                log.info("{}", priceType.getPptId());
                save(new ProductPriceType(ppt.getProductId(), priceType.getPptId()));
            }
            //有纯积分兑换方式
            if (ppt.getPptIntegral() != null && !ppt.getPptIntegral().equals(0) && integral == 0) {
                PriceType priceType = new PriceType(0, ppt.getPptIntegral());
                priceType.setPptType(1);

                priceTypeMapper.insert(priceType);
                save(new ProductPriceType(ppt.getProductId(), priceType.getPptId()));
            }
            //有混合兑换方式
            if (ppt.getPptMoneyIntegral() != null && !ppt.getPptMoneyIntegral().get(0).equals(0)
                    && !ppt.getPptMoneyIntegral().get(1).equals(0) && moneyIntegral == 0) {
                List<Integer> list = ppt.getPptMoneyIntegral();
                PriceType priceType = new PriceType(list.get(0), list.get(1));
                priceType.setPptType(2);

                priceTypeMapper.insert(priceType);
                save(new ProductPriceType(ppt.getProductId(), priceType.getPptId()));
            }
        }catch(Exception exception) {
            throw new InsertProductPriceTypeException("添加失败");
        }
        return true;
    }

    @Override
    public Integer getppt_Type(Integer productId) {
        List<ProductPriceType> products = productPriceTypeMapper.selectListById(productId);

        int money = 0, integral = 0, moneyIntegral = 0;
        for (ProductPriceType productPriceType : products) {
            Long id = productPriceType.getPriceTypeId();
            try {
                PriceType priceType = priceTypeMapper.selectOne(
                        new QueryWrapper<PriceType>().eq("ppt_id", id));
                if (priceType.getPptType() == 0) money = 1;
                if (priceType.getPptType() == 1) integral = 1;
                if (priceType.getPptType() == 2) moneyIntegral = 1;
            }catch (Exception exception) {
                throw new UpdateProductPriceException("更新失败");
            }
        }
        if (money == 1 && integral == 1 && moneyIntegral == 1) return 7;
        if (money == 1 && integral == 1 && moneyIntegral == 0) return 6;
        if (money == 1 && integral == 0 && moneyIntegral == 1) return 5;
        if (money == 0 && integral == 1 && moneyIntegral == 1) return 4;
        if (money == 1 && integral == 0 && moneyIntegral == 0) return 3;
        if (money == 0 && integral == 0 && moneyIntegral == 1) return 2;
        if (money == 0 && integral == 1 && moneyIntegral == 0) return 1;

        return 0;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updateppt_Type(UpdatePriceTypeDTO uppt) {
        List<ProductPriceType> products = productPriceTypeMapper.selectListById(uppt.getProductId());

        int money = 0, integral = 0, moneyIntegral = 0;
        if (uppt.getOperation() == 7) {money = 1;integral = 1;moneyIntegral = 1;}
        if (uppt.getOperation() == 6) {money = 1;integral = 1;
        moneyIntegral = 0;}
        if (uppt.getOperation() == 5) {money = 1;moneyIntegral = 1;
        integral = 0;}
        if (uppt.getOperation() == 4) {money = 0;
        integral = 1;moneyIntegral = 1;}
        if (uppt.getOperation() == 3) {money = 1;
        integral = 0;moneyIntegral = 0;}
        if (uppt.getOperation() == 2) {money = 0 ; integral = 0;moneyIntegral = 1;}
        if (uppt.getOperation() == 1) {money = 0;
        integral = 1;
        moneyIntegral = 0;}
        log.info("{},{},{}",money,integral,moneyIntegral);
        try {
            for (ProductPriceType productPriceType : products) {
                Long id = productPriceType.getPriceTypeId();
                LambdaQueryWrapper<PriceType> eq = new LambdaQueryWrapper<PriceType>().eq(PriceType::getPptId, id);

                PriceType priceType = priceTypeMapper.selectOne(eq);
                if (priceType.getPptType() == 0 && money == 1) {
                    priceType.setPptMoney(uppt.getPptMoney());
                    priceType.setPptIntegral(0);
                    priceType.setUpdateTime(LocalDateTime.now());
                    priceTypeMapper.update(priceType, eq);
                }
                if (priceType.getPptType() == 1 && integral == 1) {
                    priceType.setPptMoney(0);
                    priceType.setPptIntegral(uppt.getPptIntegral());
                    priceType.setUpdateTime(LocalDateTime.now());
                    priceTypeMapper.update(priceType, eq);
                }
                if (priceType.getPptType() == 2 && moneyIntegral == 1) {
                    priceType.setPptMoney(uppt.getPptMoneyIntegral().get(0));
                    priceType.setPptIntegral(uppt.getPptMoneyIntegral().get(1));
                    priceType.setUpdateTime(LocalDateTime.now());
                    priceTypeMapper.update(priceType, eq);
                }
            }
        } catch (Exception exception) {
            throw new UpdateProductPriceException("更新失败");
        }
        return true;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean delete_Type(Integer operator, Integer productId) {
        List<ProductPriceType> products = productPriceTypeMapper.selectListById(productId);

        try {
            for (ProductPriceType productPriceType : products) {
                Long id = productPriceType.getPriceTypeId();
                LambdaQueryWrapper<PriceType> eq = new LambdaQueryWrapper<PriceType>()
                        .eq(PriceType::getPptId, id);


                PriceType priceType = priceTypeMapper.selectOne(eq);
                if (priceType.getPptType().equals(operator)) {
                    priceTypeMapper.deleteById(id);
                    this.remove(new QueryWrapper<ProductPriceType>()
                            .eq("price_type_id",id));
                }
            }
        } catch (Exception exception) {
            throw new UpdateProductPriceException("删除失败");
        }
        return true;
    }
}

